/*
 * Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.templateproject.core.http;

import android.util.Log;

import com.just.agentweb.js.AgentWebJsInterfaceCompat;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xutil.common.StringUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import javax.xml.transform.Result;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class Chart {

    public String totalString;
    public static final MediaType MEDIA_TYPE_MARKDOWN
            = MediaType.parse("text/x-markdown; charset=utf-8");
    private static final MediaType FROM_DATA = MediaType.parse("multipart/form-data");

    public OkHttpClient client = new OkHttpClient();
    public void upload(String url, String filePath, String UUID,String fileName) throws Exception {
        File file2 = new File(filePath);
        Log.e("statu",filePath);

        //使用MultipartBody上传多个文件，Builder构建着
        new Thread(()->{
            Log.e("真","新");
            //创建 MediaType 设置上传文件类型
            MediaType MEDIATYPE = MediaType.parse("text/plain; charset=utf-8");
//获取请求体
            RequestBody requestBody = RequestBody.create(MEDIATYPE, file2);
//创建请求
            Request request = new Request.Builder().url(url).addHeader("UUID",UUID).addHeader("fn",fileName)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    Log.e("请求失败,异常信息为: {}", e.getMessage());
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response)
                        throws IOException {
                    Log.e("请求成功,返回信息为: {}", response.body().toString());
                }
            });


        }).start();

    }

    public String run(String url, String key1, String val1, String key2, String val2) throws Exception {
        RequestBody requestBody = new RequestBody() {
            @Override public MediaType contentType() {
                return MEDIA_TYPE_MARKDOWN;
            }

            @Override public void writeTo(BufferedSink sink) throws IOException {
                sink.writeUtf8("Numbers\n");
                sink.writeUtf8("-------\n");
                for (int i = 2; i <= 997; i++) {
                    sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
                }
            }

            private String factor(int n) {
                for (int i = 2; i < n; i++) {
                    int x = n / i;
                    if (x * i == n) return factor(x) + " × " + i;
                }
                return Integer.toString(n);
            }
        };

        Request request = new Request.Builder()
                .url(url).addHeader(key1,val1).addHeader(key2,val2)
                .post(requestBody)
                .build();
        Response response = client.newCall(request).execute();

        return response.body().string();

    }

}
